dashboard.security = {	
	authentication : {
		init : function() {
			jQuery('input[id=username]').focus();
		},
		check_login : function(){
			var username = $("#username").val();
			var password = $("#password").val();
			//Check required username and password
			if(username == ""){
				dashboard.show_alert("Username is required");	
				jQuery("#username").focus();								
				return false;
			}else if( password == ""){
				dashboard.show_alert("Password is required");	
				jQuery("#password").focus();			
				return false;
			}		
			
			//Check invalid code
			if (!dashboard.util_isValidCode(username)) {
				dashboard.show_alert("Username contains invalid code");				
				jQuery("#username").focus();
				return false;
			} else if (!dashboard.util_isValidCode_lite(password)) {
				dashboard.show_alert("Password contains invalid code");		
				jQuery("#password").focus();
				return false;
			}
			
			if(username.length >= 255){
				dashboard.show_alert("Username is too long! It must be 255 characters or less.");			
				jQuery("#username").focus();
				return false;	
			}
			if(password.length >= 255){
				dashboard.show_alert("Password is too long! It must be 255 characters or less.");			
				jQuery("#password").focus();
				return false;	
			}
			//Check Captcha
			
			dashboard.security.authentication.do_authenticate(username, password);
			return false;
		
		},
		do_authenticate : function(username, password){
			var remember_me = document.getElementById("remember_me").checked;			
			data = jQuery("form#login-form").serialize() + "&remember_me=" + remember_me;
			path = dashboard.generate_path(new Array("ajax/security.php"))+"&p=login";
			jQuery.ajax({
		        type: "POST",
		        url: path,
		        data: data,
		        dataType: "text",		        
		        success: function(result) {			        	
		        	if(result == 'success'){
		        		window.location = "index.php";
		        	}else{
		        		dashboard.show_alert_fix("#alert",result);
		        	}      	        
		 		}
			}); 			
		}
	}
};
